package com.shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.common.Result;
import com.shopping.dto.ProductDTO;
import com.shopping.dto.ProductQueryDTO;
import com.shopping.entity.Product;
import com.shopping.service.ProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/products")
@PreAuthorize("hasRole('ADMIN')")
public class AdminProductController {

    @Resource
    private ProductService productService;

    @GetMapping
    public Result<Page<ProductDTO>> getProductList(ProductQueryDTO queryDTO) {
        try {
            Page<ProductDTO> page = productService.getProductList(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("获取商品列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result<ProductDTO> getProductById(@PathVariable Long id) {
        try {
            ProductDTO product = productService.getProductById(id);
            return Result.success(product);
        } catch (Exception e) {
            return Result.error("获取商品详情失败: " + e.getMessage());
        }
    }

    @PostMapping
    public Result<ProductDTO> createProduct(@Valid @RequestBody Product product) {
        try {
            ProductDTO createdProduct = productService.createProduct(product);
            return Result.success(createdProduct);
        } catch (Exception e) {
            return Result.error("创建商品失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Result<ProductDTO> updateProduct(
            @PathVariable Long id,
            @Valid @RequestBody Product product
    ) {
        try {
            ProductDTO updatedProduct = productService.updateProduct(id, product);
            return Result.success(updatedProduct);
        } catch (Exception e) {
            return Result.error("更新商品失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteProduct(@PathVariable Long id) {
        try {
            productService.deleteProduct(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除商品失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}/status")
    public Result<Void> toggleProductStatus(@PathVariable Long id) {
        try {
            productService.toggleProductStatus(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新商品状态失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}/recommended")
    public Result<Void> toggleRecommended(@PathVariable Long id) {
        try {
            productService.toggleRecommended(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新商品推荐状态失败: " + e.getMessage());
        }
    }

    @GetMapping("/statistics")
    public Result<Object> getProductStatistics() {
        try {
            Object statistics = productService.getProductStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            return Result.error("获取商品统计信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/categories")
    public Result<List<Map<String, Object>>> getCategories() {
        try {
            List<Map<String, Object>> categories = productService.getProductCategories();
            return Result.success(categories);
        } catch (Exception e) {
            return Result.error("获取商品分类失败: " + e.getMessage());
        }
    }
}